package com.simple.core.config;

import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cache.CacheManager;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * 缓存配置，应用于读多写少的场景
 *
 * @author FanXing
 * @date 2022年12月12日 16:40
 */
@Configuration
public class CacheConfig {

    /**
     * 系统默认缓存TTL时间：5分钟
     */
    private static final Long SYSTEM_DEFAULT_EXPIRES = 5 * 60 * 1000L;

    @Bean
    @ConditionalOnMissingBean
    public CacheManager initCacheManager() {
        CaffeineCacheManager manager = new CaffeineCacheManager();
        manager.setCaffeine(Caffeine.newBuilder().expireAfterWrite(SYSTEM_DEFAULT_EXPIRES, TimeUnit.MILLISECONDS));
        return manager;
    }


}
